package org.wyn.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.wyn.mongodb.bean.Blog;
import org.wyn.mongodb.bean.Comment;
import org.wyn.mongodb.bean.User;
import org.wyn.mongodb.dao.impl.BlogDao;
import org.wyn.mongodb.dao.impl.FreshThingDao;
import org.wyn.mongodb.dao.impl.FriendRelationshipDao;
import org.wyn.mongodb.dao.impl.UserDao;
import org.wyn.mongodb.dao.impl.UserDetailDao;
import org.wyn.service.IBlogService;

import com.google.inject.Inject;
/**
 * 日志service实现层
 * @author wyn
 *
 */
public class BlogServiceImpl implements IBlogService{
	@Override
	public Comment addComment(String blogId, String content, String userName) {
		Blog blog = blogDao.findOne("blogId", new ObjectId(blogId));
		if(null == blog)//日志不存在
			return null;
		List<Comment> comments = blog.getComments();
		String realName = userDao.getRealName(userName);
		if(null == realName)//指定的评论者不存在
			return null;
		Comment c = new Comment(userName,realName,content);
		comments.add(c);
		blogDao.save(blog);
		blogDao.ensureIndexes();
		return c;
	}
	@Override
	public boolean writeBlog(Blog blog, User author) {
		if(StringUtils.isEmpty(blog.getContent())){
			//日志内容为空,创建失败
			return false;
		}
		//为日志添加其他属性
		blog.setAuthorUserName(author.getUserName());
		blog.setAuthorRealName(author.getRealName());
		blog.setAuthorPortrait(userDetaiDao.findPortrait(author.getUserName()));//保存头像
		blog.setCreateTime(System.currentTimeMillis());
		blog.setLastModifyTime(System.currentTimeMillis());
		blog.setViewNum(0);
		blog.setComments(new ArrayList<Comment>());
		blogDao.save(blog);//保存日志
		freshThingDao.saveThingWriteBlog(blog, author.getUserName(), author.getRealName());//保存新鲜事
		return false;
	}
	@Override
	public List<Blog> findBlogOfFriendAndSefl(String userName) {
		List<String> users = friendRelationshipDao.findRelationshipByUser(userName);
		users.add(userName);
		return blogDao.createQuery().filter("authorUserName in", users).order("-createTime").asList();
	}
	@Inject
	private BlogDao blogDao;
	@Inject
	private UserDao userDao;
	@Inject
	private UserDetailDao userDetaiDao;
	@Inject
	private FreshThingDao freshThingDao;
	@Inject
	private FriendRelationshipDao friendRelationshipDao;
	public FreshThingDao getFreshThingDao() {
		return freshThingDao;
	}
	public void setFreshThingDao(FreshThingDao freshThingDao) {
		this.freshThingDao = freshThingDao;
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public BlogDao getBlogDao() {
		return blogDao;
	}
	public void setBlogDao(BlogDao blogDao) {
		this.blogDao = blogDao;
	}
	public FriendRelationshipDao getFriendRelationshipDao() {
		return friendRelationshipDao;
	}
	public void setFriendRelationshipDao(FriendRelationshipDao friendRelationshipDao) {
		this.friendRelationshipDao = friendRelationshipDao;
	}
	public UserDetailDao getUserDetaiDao() {
		return userDetaiDao;
	}
	public void setUserDetaiDao(UserDetailDao userDetaiDao) {
		this.userDetaiDao = userDetaiDao;
	}
}
